package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.dto.PageDTO;
import com.leyou.common.exception.LyException;
import com.leyou.item.entity.Brand;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import com.leyou.user.dto.BrandDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    private final CategoryBrandService categoryBrandService;

    public BrandServiceImpl(CategoryBrandService categoryBrandService) {
        this.categoryBrandService = categoryBrandService;
    }

    @Override
    public PageDTO<BrandDTO> queryBrandByPage(String key, Integer page, Integer rows) {
        int current = Math.max(page,1);
        int size = Math.max(rows,5);
        //关键字过滤
        Page<Brand> result = query().like(StringUtils.isNotBlank(key), "name", key)
                .or().eq(StringUtils.isNotBlank(key), "letter", key)
                //分页
                .page(new Page<>(current, size));
        //解析结果
        long total = result.getTotal();
        long pages = result.getPages();
        //返回
        List<Brand> list = result.getRecords();
        return new PageDTO<>(total,pages, BrandDTO.convertEntityList(list));
    }

    @Override
    @Transactional
    public void saveBrand(BrandDTO brandDTO) {
        //新增品牌
        Brand brand = brandDTO.toEntity(Brand.class);
        save(brand);
        //新增中间表
//        List<Long> categoryIds = brandDTO.getCategoryIds();
//        Long brandId = brand.getId();
//        List<CategoryBrand> categoryBrands = new ArrayList<>();
//        for (Long categoryId : categoryIds) {
//            CategoryBrand categoryBrand = CategoryBrand.of(categoryId, brandId);
//            categoryBrands.add(categoryBrand);
//        }


        List<CategoryBrand> categoryBrands = brandDTO.getCategoryIds().stream()
                .map(cid -> CategoryBrand.of(cid, brand.getId()))//映射器map
                .collect(Collectors.toList());//收集器
        categoryBrandService.saveBatch(categoryBrands);
    }

    @Override
    public List<BrandDTO> queryBrandOfCategory(Long categoryId) {
        BrandMapper brandMapper = getBaseMapper();
        List<Brand> list = brandMapper.queryByCategoryId(categoryId);

        return BrandDTO.convertEntityList(list);
    }

    @Override
    @Transactional
    public void updateBrand(BrandDTO brandDTO) {

        boolean success = updateById(brandDTO.toEntity(Brand.class));
        if (!success){
            throw new LyException(500,"更新品牌失败!");
        }
        success = categoryBrandService.remove(new QueryWrapper<CategoryBrand>()
        .eq("brand_id",brandDTO.getId()));
        if (!success){
            throw new LyException(500,"更新品牌失败,删除中间表数据错误");
        }
        List <CategoryBrand> list = brandDTO.getCategoryIds().stream()
                .map(id -> CategoryBrand.of(id,brandDTO.getId()))
                .collect(Collectors.toList());
        categoryBrandService.saveBatch(list);
    }
}























